package com.sky.controller.admin;


import com.sky.anno.CleanCache;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.enumeration.CleanCacheType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    DishService dishService;


    @PostMapping
    @ApiOperation("添加菜品")
    //@CleanCache(CleanCacheType.ADD)
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.categoryId")
    public Result<Void> addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品信息>: {}", dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> dishPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询>: {}", dishPageQueryDTO);
        PageResult pageResult = dishService.dishPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        log.info("根据id查询菜品 id>:{}", id);
//        dishService.getDishById(id);
        return Result.success(new DishVO());
    }


    @PutMapping
    @ApiOperation("修改菜品")
    //@CleanCache(CleanCacheType.ELSE)
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result<Void> editDish(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品信息>: {}", dishDTO);
//        dishService.editDish(dishDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    //@CleanCache(CleanCacheType.ELSE)
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result<Void> deleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品>: {}", ids);
//        dishService.deleteDish(ids);
        return Result.success();
    }
}
